package com.company.cloud.article.modules.app.service.impl;

import javax.annotation.Resource;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.company.cloud.article.modules.base.entity.ArticleCategoryMappingInfo;
import com.company.cloud.article.modules.base.service.ArticleCategoryMappingInfoService;
import com.company.cloud.article.modules.cache.models.ArticleCategoryModel;
import com.company.cloud.article.modules.cache.service.ArticleCacheService;
import com.company.cloud.common.constant.BaseConstant;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.company.cloud.article.modules.cache.models.ArticleModel;
import com.company.cloud.article.modules.app.controller.req.ArticleQuery4PageParam;
import com.company.cloud.article.modules.app.service.ArticleAppService;
import com.company.cloud.article.modules.mgr.mapper.ArticleMgrMapper;

import lombok.extern.slf4j.Slf4j;

import java.util.*;

@Slf4j
@Service
public class ArticleAppServiceImpl implements ArticleAppService {

    @Resource
    private ArticleMgrMapper articleMgrMapper;
    @Autowired
    private ArticleCacheService articleCacheService;
    @Autowired
    private ArticleCategoryMappingInfoService articleCategoryMappingInfoService;

    @Override
    public IPage<ArticleModel> queryArticle(ArticleQuery4PageParam params, String companyNo) {
        IPage<ArticleModel> data=null;
        if (StringUtils.hasText(params.getCategoryId()) || StringUtils.hasText(params.getCategoryType())) {
            QueryWrapper qw = new QueryWrapper<ArticleModel>()
                    .eq("t3.category_id", params.getCategoryId())
                    .eq("t4.type",params.getCategoryType())
                    .eq(StringUtils.hasText(params.getCommentStatus()), "t1.comment_status", params.getCommentStatus())
                    .like(StringUtils.hasText(params.getTitle()), "t1.title", params.getTitle())
                    .eq(StringUtils.hasText(params.getTag()),"t2.tags", "," + params.getTag()+",")
                    .eq(StringUtils.hasText(params.getStyle()), "t1.style", params.getStyle())
                    .ne(StringUtils.hasText(params.getArticleRelNo()),"t2.article_rel_no", params.getArticleRelNo())
                    .like(StringUtils.hasText(params.getShowChannel()),"t2.show_channel",params.getShowChannel())
                    .eq(StringUtils.hasText(params.getState()),"t2.state",params.getState())
                    .eq("t2.rows_state", BaseConstant.EnabledEnum.ENABLED.getCode())
                    .eq("t2.company_no", companyNo)
                    .orderByDesc("t2.orders");

            if (BaseConstant.YNEnum.YES.getCode().equals(params.getDetail())) {
                 data=articleMgrMapper.queryArticleFullByCategory(new Page<>(params.getPage(), params.getRows()),qw);
            } else {
                data= articleMgrMapper.queryArticleByCategory(new Page<>(params.getPage(), params.getRows()),qw);
            }
        } else {
            QueryWrapper qw = new QueryWrapper<ArticleModel>()
                    .eq(StringUtils.hasText(params.getCommentStatus()), "t1.comment_status", params.getCommentStatus())
                    .like(StringUtils.hasText(params.getTitle()), "t1.title", params.getTitle())
                    .eq(StringUtils.hasText(params.getTag()),"t2.tags", "," + params.getTag()+",")
                    .eq(StringUtils.hasText(params.getStyle()), "t1.style", params.getStyle())
                    .ne(StringUtils.hasText(params.getArticleRelNo()),"t2.article_rel_no", params.getArticleRelNo())
                    .like(StringUtils.hasText(params.getShowChannel()),"t2.show_channel",params.getShowChannel())
                    .eq(StringUtils.hasText(params.getState()),"t2.state",params.getState())
                    .eq("t2.rows_state", BaseConstant.EnabledEnum.ENABLED.getCode())
                    .eq("t2.company_no", companyNo)
                    .orderByDesc("t2.orders");
            if (BaseConstant.YNEnum.YES.getCode().equals(params.getDetail())) {
                data=  articleMgrMapper.queryArticleFull(new Page<>(params.getPage(), params.getRows()),qw);
            } else {
                data=  articleMgrMapper.queryArticle(new Page<>(params.getPage(), params.getRows()),qw);
            }
        }

        if(data!=null && CollUtil.isNotEmpty(data.getRecords())){
            Set<String> articleNos=new HashSet<>();
            data.getRecords().forEach(item->{
                articleNos.add(item.getArticleNo());
            });

            List<ArticleCategoryMappingInfo> acmList= articleCategoryMappingInfoService.list(
                    new QueryWrapper<ArticleCategoryMappingInfo>()
                    .in("article_no",articleNos)
            );

            Map<Integer,ArticleCategoryModel> categoryMap =new HashMap<>();
            List<ArticleCategoryModel>  categoryList= articleCacheService.getArticleCategoryByCompanyDB(companyNo);
            if(CollUtil.isNotEmpty(categoryList)){
                categoryList.forEach(item->{
                    categoryMap.put(item.getId(),item);
                });
            }

            for (ArticleModel record : data.getRecords()) {
                if(CollUtil.isNotEmpty(acmList)){
                    acmList.forEach(item ->{
                        if(record.getArticleNo().equals(item.getArticleNo())){
                            ArticleCategoryModel articleCategoryModel = categoryMap.get(item.getCategoryId());
                            record.addCategoryObj(articleCategoryModel);
                        }
                    });
                }
            }


        }






        return data;


    }
}
